unit fMain;

interface

uses
  Classes, Controls, Dialogs, ExtCtrls, Forms, Graphics, IniFiles, Messages, ScktComp,
  Variants, SysUtils, Windows,
  uCanvasState, uNetworkInterface, uRailroadYard;

const
  CURSOR_HEIGHT = 12;
  CURSOR_WIDTH = 10;

type
  TMainFrm = class(TForm)
    trackLayoutImg: TImage;
    // message handlers (need to be first!)
    procedure FormCreate(Sender: TObject);
    procedure trackLayoutImgMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);

    // attributes
    private
      aIniFile: TIniFile;
      aNetwork: TNetworkInterface;
      aRailroadYard: TRailroadYard;
      aRaster: byte;
      aCursorX,
      aCursorY: Cardinal;
      aUnderCursor: TBitmap;

    // private operations
    private
      procedure HideCursor;
      procedure DrawCursor(paClear: boolean = false);
  end;

var
  MainFrm: TMainFrm;

implementation


{$R *.dfm}

procedure TMainFrm.FormCreate(Sender: TObject);
begin
  aIniFile := TIniFile.Create('data/config.ini');
  aRailroadYard := TRailroadYard.Create(trackLayoutImg.Canvas);
  aNetwork := TNetworkInterface.Create(aIniFile.ReadInteger('network','port',80), aRailroadYard);
  aRailroadYard.LoadFromFile('data/railroadYard.xml', true);
  aRailroadYard.Draw;
end;


procedure TMainFrm.trackLayoutImgMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  case Button of

    mbLeft:
    begin
      HideCursor;
      aRailroadYard.LeftMouseClicked(x,y);
      DrawCursor;
    end;

    mbRight:
    begin
      HideCursor;
      aRailroadYard.RightMouseClicked(x,y);
      DrawCursor;
    end;

    mbMiddle:
    begin
      HideCursor;
      aRailroadYard.MiddleMouseClicked(x,y);
      DrawCursor;
    end;
  end;
end;


procedure TMainFrm.HideCursor;
begin
  DrawCursor(true);
end;


procedure TMainFrm.DrawCursor(paClear: boolean);
var
  canvas: TCanvas;
  originalCanvasState: TCanvasState;
  x,y: Integer;
  cursorRect: TRect;
begin
{
  canvas := trackLayoutImg.Canvas;
  originalCanvasState.CopyFromCanvas(canvas);

  x := (aCursorX) * (aRaster div 2);
  y := (aCursorY) * (aRaster div 2);


  cursorRect := Rect(x - CURSOR_WIDTH div 2,
                     y - CURSOR_HEIGHT div 2,
                     x + CURSOR_WIDTH div 2,
                     y + CURSOR_HEIGHT div 2);

  if paClear then
  begin
    canvas.CopyRect(cursorRect,aUnderCursor.Canvas,aUnderCursor.Canvas.ClipRect);
  end
  else begin
    //save whats under the cursor
    aUnderCursor.Canvas.CopyRect(aUnderCursor.Canvas.ClipRect, canvas, cursorRect);
    //draw cursor
    canvas.Pen.Width := 1;
    canvas.Brush.Color := clFuchsia;
    canvas.Pen.Color := clFuchsia;
    canvas.Pen.Mode := pmMerge;
    canvas.Rectangle(cursorRect);
  end;

  originalCanvasState.CopyToCanvas(canvas);
  }
end;


end.

